const App = getApp()
const util = require('../../../utils/util.js')
Page({
  data: {
    date: '',
    time: '',
    userInfo: {
      consignee: '', // 联系人 
      phone: '', // 联系电话 
      appointmentTime: '', // 预约时间 
    }
  },
  onLoad (option) {
    console.log(option)
    let defaultCar = wx.getStorageSync('defaultCar')
    let products = wx.getStorageSync('group')
    let userId = wx.getStorageSync('userId')
    let agent = wx.getStorageSync('agent')
    let nowDate = new Date().getTime()

    this.setData({
      merchant: option,
      defaultCar,
      products,
      userId,
      
      agent,
      'userInfo.consignee': defaultCar.linkman,
      'userInfo.phone': defaultCar.phone,
      date: util.formatTime(new Date(nowDate+24*60*60*1000)).date,//; 时间推后24小时,
      time: util.formatTime(new Date()).hhmm,
    })
  },
  bindDateChange (e) { // 预约日期
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange (e) { // 预约时间
    this.setData({
      time: e.detail.value
    })
  },
  inputBlur (e) { // 失去焦点时间
		let { key } = e.currentTarget.dataset
		let { value } = e.detail
    this.setData({ [key]: value })
  },
  addOrder (e) {
    let {userInfo, date, time, agent, userId, merchant, products} = this.data
    if (userInfo.consignee == '') {
      this.alert('请输入联系人')
      return false
    }
    if (userInfo.phone == '') {
      this.alert('请输入联系电话')
      return false
    }
    userInfo.appointmentTime = date + ' ' + time
      

      
    let params = {
      buyerId: userId, 
      sellerId: agent.id,
      multiGroupSetId: products.multiGroupSetId,
      groupId: wx.getStorageSync('zhuangtai'),
      shopId: merchant.merchantId,
      fromId: e.detail.formId,
      userInfo
    }



    App.HttpService.multiOrderSave(params).then(res => {
      let data = res.data
      if (data.code == 200) {
        let config = data.data
        let payconfig = {
          timeStamp: config.timeStamp,
          nonceStr: config.nonceStr,
          package: config.package,
          signType: config.signType,
          paySign: config.paySign,
        }
        payconfig.success = () => {
          wx.navigateTo({
            url: '/pages/group-buying/order-list/index?stauts=0&back=1'
          })
          wx.removeStorageSync('group')
        }
        wx.requestPayment(payconfig)
      } else {
        wx.showModal({
          title: '提示',
          content: data.msg,
          showCancel: false
				})
      }
    }, e => {
      wx.showModal({
        title: '提示',
        content: '服务器错误',
        showCancel: false
      })
    })
  },
  alert (msg) {
    wx.showToast({
      title: msg,
      image: '../../../assets/images/warning.png',
      duration: 1500
    })
  },
})